import jsonp from 'common/js/jsonp'
import {
  CommendParam,
  CommendOptions
} from './config'
import axios from 'axios'

// 获取热门搜索
export function getHotKey() {
  let url = 'https://c.y.qq.com/splcloud/fcgi-bin/gethotkey.fcg'
  const data = Object.assign({}, CommendParam, {
    // 移动端
    platform: 'h5',
    needNewCode: 1
  })
  return jsonp(url, data, CommendOptions)
}
export function kougoSearch(query = '', page = 1, has = true, n = 20) {
  let url = 'http://mobilecdn.kugou.com/api/v3/search/song'
  const data = Object.assign({}, {
    format: 'jsonp',
    // 移动端
    keyword: query,
    page,
    pagesize: n,
    showtype: 1,
    callback: 'kgJSONP500324885'
  })
  let DisListOptions = Object.assign({}, {
    // 前缀
    // prefix: 'recom',
    // 回调名称
    name: 'kgJSONP500324885'
  }, CommendOptions)
  return jsonp(url, data, DisListOptions)
}
/**
 * 网易云 搜索 被跨域封锁
 */
export function search(query = '', page = 1, has = true, n = 20) {
  let url = 'api/search'
  const data = Object.assign({}, CommendParam, {
    // 移动端
    platform: 'h5',
    needNewCode: 1,
    w: query,
    zhidaqu: 1,
    // 是否有歌手
    catZhida: has ? 1 : 0,
    t: 0,
    flag: 0,
    ie: 'utf-8',
    sem: 0,
    aggr: 0,
    perpage: n,
    n,
    p: page,
    remoteplace: 'txt.mqq.all'
  })
  return axios.get(url, {
    params: data
  }).then((res) => {
    return Promise.resolve(res.data)
  })
}

// 获取酷狗歌曲
export function getSongKuGou(hash) {
  let url = 'api/kugou/get'
  const data = Object.assign({}, {
    cmd: 'playInfo',
    hash,
    from: 'mkugou'
  })
  return axios.get(url, {
    params: data
  }).then((res) => {
    return Promise.resolve(res.data)
  })
}

// 获取酷狗歌词
export function getLyricKuGou(hash, keyword, timelength) {
  let url = 'api/kugou/lyric'
  const data = Object.assign({}, {
    cmd: '100',
    keyword,
    hash,
    timelength,
    d: Math.random()
  })
  return axios.get(url, {
    params: data
  }).then((res) => {
    return Promise.resolve(res.data)
  })
}
